﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.WeiXin;
using Xproject.Framework.Utility.Extensions;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.ExceptionHandling;
using Xproject.Framework.Core;
using System.Linq.Expressions;

namespace Repository.WeiXinProject
{
    public static class WeiXinTokenRep
    {
        public static WeiXinToken Get(this IRepositoryAsync<WeiXinToken> repository, string ID)
        {
            Expression<Func<WeiXinToken, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(ID))
            {
                express = express.AndAlso(i => i.AppID == ID);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }
        
        public static WeiXinToken GetWeiXinToken(this IRepositoryAsync<WeiXinToken> repository, string appId, string siteName)
        {
            Expression<Func<WeiXinToken, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(appId))
            {
                express = express.AndAlso(i => i.AppID == appId);
            }            
            if (string.IsNullOrEmpty(siteName))
            {
                express = express.AndAlso(i => i.SiteName == siteName);
            }
            
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }
    }
}
